<?php

require_once(dirname(dirname(__FILE__)) . '/include/core.php');

$fields = json_decode(file_get_contents('fields.json'),true);

#如果有多余的字段，直接报错
$errors = array();

if(!isset($data['orders'])){
	send(DATA_ERROR);
}

if(count($data['orders']) > 500){
	send(NUMBER_ERROR);
}

$data = $data['orders'];

if(!count($data)){
	send(NUMBER_ERROR);
}

if(is_assoc($data)){
	send(DATA_ERROR);
}


for($i = 0; $i < count($data); $i++){
	#如果不存在order_id，直接报错
	if(!isset($data[$i]['order_id'])){
		unset($data[$i]);
		file_put_contents('log.txt','no order_id',FILE_APPEND);
		continue;
	}

	$data[$i]['store_id'] = $has_store['store_id'];
	$data[$i]['store_id_first'] = $has_store['store_id_first'];
	$data[$i]['store_id_second'] = $has_store['store_id_second'];
	$data[$i]['datetime_input'] = date('Y-m-d H:i:s');

	$error = check_single_item($data[$i],$fields);

	#如果有错误
	if($error){
		$errors[] = $data[$i]['order_id'];
		unset($data[$i]);
		//file_put_contents('log.txt','check_error',FILE_APPEND);
		continue;
	}

	
	#查找该订单是否存在
	$has_this_order = Model()->table('order')->where(array('order_id' => $data[$i]['order_id'],'store_id'=> $has_store['store_id']))->find();
	if($has_this_order){
		$data[$i]['id'] = $has_this_order['id'];
	}

	#订单检查通过后，检查商品
	if(!isset($data[$i]['order_goods']) 
		#数组为空
		|| !count($data[$i]['order_goods']) 
		#不是个数组
		|| !is_array($data[$i]['order_goods'])
		#是个关联数组
		|| is_assoc($data[$i]['order_goods'])
	){
		$errors[] = $data[$i]['order_id'];
		unset($data[$i]);
		if(!isset($data[$i]['order_goods'])){
			file_put_contents('log.txt','error type 1',FILE_APPEND);
		}
		if(is_assoc($data[$i]['order_goods'])){
			file_put_contents('log.txt','error type 4',FILE_APPEND);
			
			
		}
		
		continue;
	}

	#检查通过后，将订单编号补完成为唯一编号	
	//$after_fix = (string)$has_store['store_id'];
	//for($n = strlen($after_fix); $n < 4; $n++){
	//	$after_fix = '0' . $after_fix;
	//}
	//$data[$i]['orderNo'] .= $after_fix;


	$codes = array('ProvinceCode','CityCode','DistrictCode');
	$areas = array('Province','City','District');
	foreach($codes as $j => $code){

		if(empty($data[$i][$code])){
			$item = Model()->query("select area_code from yoehi.ra_area where area_name = '".$data[$i][$areas[$j]]."'");
			if(count($item)){
				$data[$i][$code] = $item[0]['area_code'];
			}
		}
	}

	if(empty($data[$i]['shipper'])){
		$data[$i]['shipper'] = $has_store['store_shipper'];
	}
	if(empty($data[$i]['shipperAddress_1'])){
		$data[$i]['shipperAddress_1'] = $has_store['store_shipperAddress'];
	}
	if(empty($data[$i]['shipperTelephone'])){
		$data[$i]['shipperTelephone'] = $has_store['store_shipperTelephone'];
	}
	if(empty($data[$i]['shipperCountry'])){
		$data[$i]['shipperCountry'] = $has_store['store_shipperCountry'];
	}
	if(empty($data[$i]['order_state'])){
		$data[$i]['order_state'] = 20;
	}

	switch ($_SESSION['this_user']['authorization']) {
		case 2:
		case 3:
			$data[$i]['status_shangjian'] = 0;
			$data[$i]['status_haiguan'] = 0;
			$data[$i]['status_pay'] = 0;
			break;
		case 4:
			$data[$i]['status_shangjian'] = 5;	
			$data[$i]['status_haiguan'] = 5;	
			$data[$i]['status_pay'] = 5;	
			break;
	}

	$data[$i]['status_shangjian_yundan'] = 0;
	$data[$i]['status_haiguan_yundan'] = 0;


	$data[$i]['is_tax'] = 1;
	
	// $data[$i]['orderNo'] = $data[$i]['store_id'] . '_' . $data[$i]['orderNo'];



	foreach($data[$i]['order_goods'] as $k => $v){
		$this_goods = Model('goods_stock')->where(array('goods_id' => $v['goods_id'],'store_id'=> $store_id))->find();
		//print_r($this_goods);
		//echo $store_id;
		if(!$this_goods 
			// || empty($v['order_id']) 
			|| empty($v['goods_id'])
			|| empty($v['qty']) 
			|| empty($v['price']) 
			|| empty($v['total']) 
			// || empty($v['giftFlag'])
			|| $v['price'] * $v['qty'] != $v['total']
		){
			$errors[] = $data[$i]['order_id'];
			if(!$this_goods){
				file_put_contents('log.txt','goods error type 0',FILE_APPEND);
			}
			if(!$this_goods){
				file_put_contents('log.txt','goods error type 0',FILE_APPEND);
			}
			if(!$this_goods){
				file_put_contents('log.txt','goods error type 0',FILE_APPEND);
			}
			if(!$this_goods){
				file_put_contents('log.txt','goods error type 0',FILE_APPEND);
			}
			if(!$this_goods){
				file_put_contents('log.txt','goods error type 0',FILE_APPEND);
			}
			unset($data[$i]);
			break;
		}
		$data[$i]['order_goods'][$k]['order_id'] = $data[$i]['order_id'];
		$data[$i]['order_goods'][$k]['store_id_first'] = $has_store['store_id_first'];
		$data[$i]['order_goods'][$k]['store_id_second'] = $has_store['store_id_second'];
	}


}


if(!count($data)){
	send(ALL_ERROR);
}

#全部通过，插入数据库
$model = Model()->table('order');
$model_og = Model()->table('order_goods');
$suppose = count($data);

foreach($data as $key => $val){
	if(empty($val)){
		continue;
	}
	$order_goods = $val['order_goods'];
	unset($val['order_goods']);
	$this_order = Model()->table('order')->where(array('order_id' => $val['order_id'],'store_id_first' => $store_id))->find();
	if($this_order){
		$val['id'] = $this_order['id'];
	}
	$id = $model->insert($val,true);
	if(!$id){
		$errors[] = $data[$i]['goods_id'];
		continue;
	}
	#通过的情况，删删除原有的商品，并重新添加
	Model('order_goods')->where(array('ds_order_id' => $id))->delete();
	foreach($order_goods as $g){
		$g['ds_order_id'] = $id;
		unset($g['order_goods_id']);
		$_id = $model_og->insert($g);
		//如果一个商品插入失败，则清除该订单记录
		if(!$_id){
			Model()->table('order_goods')->where(array('ds_order_id' => $id))->delete();
			Model()->table('order')->where(array('ds_order_id' => $id))->delete();
			$errors[] = $val['order_id'];
			break;
		}
	}
}
if(count($errors) == $suppose){
	send(ALL_ERROR);
}
else if(count($errors)){
	send(SOME_SUCCESS,$errors);
}
else{
	send(RESPONSE_SUCCESS);
}


